<?php

namespace App\Models;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Handroom extends Model
{
    use SoftDeletes, Filterable;

    protected $fillable = ['community_id','communityname','province','provincename','region','regionname','area','areaname','town_id','town','title','room','hall','kitchen','toilet','acreage','price','category_id','address',
        'floor','floor_num','map','longitude','latitude','renovation','contact_name','contact_tel','supporting','pm_type','tags','rec','daytime','thumb','images','average',
        'overall_view','content','seo_title','seo_keywords','seo_description','status','year','direction','renovation_name','metro_id','metro_station_id','follow'
    ];

    //所属分类，朝向
    public function category()
    {
        return $this->belongsTo('App\Models\Category');
    }

    //所属小区
    public function community()
    {
        return $this->belongsTo('App\Models\Community');
    }

    public function attribute()
    {
        return $this->belongsToMany('App\Models\Attribute','attribute_properties','property_id')->where('type','hand');
    }

    public function getSupportingAttribute($value)
    {
        return unserialize($value);
    }

    public function getImagesAttribute($value)
    {
        return unserialize($value);
    }

    public function getTagsAttribute($value)
    {
        return explode(',',$value);
    }

    public function scopeValid($query,$sort)
    {
        switch ($sort)
        {
            case 'asc':
                $model = $query->orderBy('average','asc');
                break;
            case 'desc':
                $model = $query->orderBy('average','desc');
                break;
            case 'popular':
                $model = $query->orderBy('follow','desc');
                break;
            default:
                $model = $query->orderBy('created_at','desc');
        }
        return $model;
    }

}
